


strLengthValidator();//验证输入长度
(function(){
	var loadDiv = null;
	$(function () {
		loadDiv = $("#loadDiv").loading();
		initViewWindow();//初始化短信发送窗口	
	});
	
	
	/**
	 * 初始化短信发送窗口
	 * @returns {Function}
	 */
	
	function initViewWindow(){
		//声明消息提示
	    var toolTips = new Object();
	
	    //设置表单验证
	    var validator = $("#sendMessagesForm").validate({
	        rules:{
	        	receiveName:{
	        		required:true,
	        	},
	        	messageContent:{
	        		required:true,
	        		strMaxLenth:"900",
	        	}
	        },
	        messages:{
	            
	        },
	        errorPlacement: function(eror,element){
	            var toolTip = createTooltip(toolTips,element.attr("id"));
	            toolTip.setContent(eror.text());
	            toolTip.close();
	            element.addClass("error-border");
	        },
	        success:function(label){
	            var elementId = label.attr("id").substring(0,label.attr("id").length - 6);
	            var toolTip = toolTips[elementId];
	            $("#" + elementId).removeClass("error-border");
	            toolTip.close();
	            toolTip.detach();
	        },
	        submitHandler:function(form){
	        	loadDiv.showLoading();
	        	post(getPath() + "base/userPhone/sendMessage",
	        		$("#sendMessagesForm").serialize(),
	                function(resultObject){
	                var errors = resultObject.errors;
	                var results =eval("("+ resultObject.result  +")"); 
	               // alert(results.status);
	                //判断后台是否有返回错误消息
	                if(errors && errors.length > 0){
	                	loadDiv.hideLoading();
	                    for(var i=0; i < errors.length; i++){
	                        var error = errors[i];
	                        var toolTip = createTooltip(toolTips,error.field);
	                        toolTip.setContent(error.defaultMessage);
	                        $("#" + error.field).addClass("error-border");
	                    }
	                }else{
	                	loadDiv.hideLoading();
	                    //返回消息，弹出添加成功提示框
	                	createWindow(results.status,{callBack:msgCallBack}).dialog("open");
	                	
	                }
	        	},function(resultObject){
	        		loadDiv.hideLoading();
	        		createWindow(resultObject).dialog('open');
	        	});
	        }
	    });
		//取消
		$('#cancleBtn').click(function(){
			$("#mobileGroundTitleUl li").css('background-color','white');
			$("#mobileGroundTitleUl li span").removeClass('selected');
			$("#allPhoneDiv").css('background-color','#D9E8FF');
			$("#allPhoneDiv span").addClass('selected');
			//$("#allPhoneDiv").css('color','#478bf1');
			$("#groundId").val("");
			$("#dieleteGroundId").val("");
			$("#rightDiv").load(getPath() + "base/userPhone/list/1/"+pageSize);
		});
		
		$("#messageUserWindow").click(function(event){
			event.preventDefault();
			var userIds = $("#receiveIds").val();
			var status = "1,2";
			//var status = "2";
			loadDiv.showLoading();
			$("#userListSelect").load(getPath() + 'base/user/list/choose?callBackFun=userCallBack&isMany=1&status='+status+'&userIds='+userIds);
		})
	}
	
	
})();
function msgCallBack(){
	$("#rightDiv").load(getPath() + "base/userPhone/list/1/"+pageSize);
}
function userCallBack(selectList){
	var receiveName = new Array();
	var receiveIds = new Array();
	if(selectList && selectList.length){
		for ( var i = 0; i < selectList.length; i++) {
			var user = selectList[i];
			receiveName[i]=user.nickname;
			receiveIds[i]=user.id;
		}
	}
	$('#receiveName').val(receiveName.join(","));
	$('#receiveIds').val(receiveIds.join(","));
	 $("#sendMessagesForm").valid();
}